<?php
/**
 * @author: Đặng Văn Trường
 * @since: 22/07/2014
 * @todo : Model Van ban den
 */
class module_models_Record extends Zend_Db_Table {	
	
	protected $_name="record";
    protected $_primary_name;
    protected $_primary = "RecordID";
    
    public function __construct()
    {
        parent::__construct();
        $this->_primary_name = $this->_primary;
    }
	
    public function getAll($params) {
        $pagenum    = $params['pagenum'];
        $pagesize   = $params['pagesize'];
        $start      = $pagenum * $pagesize;
        $where = "1=1";
        $srecordtype = $params['srecordtype'];
        $sform      = $params['sform'];
        $fromDate   = $params['fromDate'];
        $toDate     = $params['toDate'];
		if(!$params['View']){
			$StaffID = $params['StaffID'];
			$where .= " And StaffID ='$StaffID'";
		}
        if ($srecordtype !='') {
            $where .= " And RecordType ='$srecordtype'";
        }
        if ($sform !='') {
            $where .= " And Form ='$sform'";
        }
        if ($fromDate !='') {
            $objDate = new Clound_Datetime($fromDate, 'dd/MM/yyyy');
            $fromDate = $objDate->ToYYyymmdd();
            $where .= " And CreateDate >= '$fromDate'";
        }
        if ($toDate !='') {
            $objDate = new Clound_Datetime($toDate, 'dd/MM/yyyy');
            $objDate->addDay('1');
            $toDate = $objDate->ToYYyymmdd();
            $where .= " And CreateDate < '$toDate'";
        }
        // Filter
        if (isset($params['filterscount']))
        {
            $filterscount = $params['filterscount'];
            
            if ($filterscount > 0)
            {
                $where .= " And (";
                $tmpdatafield = "";
                $tmpfilteroperator = "";
                for ($i=0; $i < $filterscount; $i++)
                {
                    // get the filter's value.
                    $filtervalue = $params["filtervalue" . $i];
                    // get the filter's condition.
                    $filtercondition = $params["filtercondition" . $i];
                    // get the filter's column.
                    $filterdatafield = $params["filterdatafield" . $i];
                    // get the filter's operator.
                    $filteroperator = $params["filteroperator" . $i];
                    
                    if ($tmpdatafield == "")
                    {
                        $tmpdatafield = $filterdatafield;           
                    }
                    else if ($tmpdatafield <> $filterdatafield)
                    {
                        $where .= ")AND(";
                    }
                    else if ($tmpdatafield == $filterdatafield)
                    {
                        if ($tmpfilteroperator == 0)
                        {
                            $where .= " AND ";
                        }
                        else $where .= " OR ";  
                    }
                    
                    // build the "WHERE" clause depending on the filter's condition, value and datafield.
                    $where .= Clound_Public::getInstance()->getClauseByFilter($filtercondition, $filterdatafield, $filtervalue);
                                    
                    if ($i == $filterscount - 1)
                    {
                        $where .= ")";
                    }
                    
                    $tmpfilteroperator = $filteroperator;
                    $tmpdatafield = $filterdatafield;      
                }
            }
        }
        // Sort
        $orders =' CreateDate DESC';
        if (isset($params['sortdatafield'])) {
            $sortfield = $params['sortdatafield'];
            $sortorder = $params['sortorder'];
            if ($sortorder != '') {
                if ($sortorder == "desc") {
                    $orders = " " . $sortfield . " DESC";
                } else if ($sortorder == "asc") {
                    $orders = " " . $sortfield . " ASC";
                }
            }
        }
        try {
            // Lay tong
            $getTotal = $this->select()->from($this, array())->columns(array('total' => new Zend_Db_Expr('COUNT(*)')))->where($where);
            $rows = $this->getAdapter()->fetchRow($getTotal);
            $total_rows = $rows['total'];
            // Lay du lieu
            $query = $this->select()->from($this)->where($where)->order($orders)->limit($pagesize, $start);
            $rows = $this->getAdapter()->fetchAll($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
		if($total_rows>0){
			$oCacheFile = new Clound_CacheFile();
			$oCloundPublic = new Clound_Public();
			$arrRecordType = $oCacheFile->getList('TTHC');
			$arrForm = $oCacheFile->getList('HINH_THUC');
			for($i=0;$i<$total_rows;$i++){
				//Loai TTHC
				$rows[$i]['RecordTypeCode'] = $rows[$i]['RecordType'];
				$rows[$i]['FormCode'] = $rows[$i]['Form'];
				$rows[$i]['RecordType'] = $oCloundPublic->getNameByCode($arrRecordType, $rows[$i]['RecordType']);
				//Hinh thuc
				$rows[$i]['Form'] = $oCloundPublic->getNameByCode($arrForm, $rows[$i]['Form']);
			}
		}		
        $data[] = array(
            'TotalRows' => $total_rows,
            'Rows' => $rows
        );

        return $data;
    }
    public function selectRow($id){
        if($id){
            $query = $this->select()->from($this)->where($this->_primary_name.' = ?', $id);
            $rows = $this->getAdapter()->fetchRow($query);
        }else{
            $rows = array($this->_primary_name =>''
                        );
        }
        return $rows;
    }

    public function deleteRows($listId){
        $aListID = explode(',', $listId);
        try {
            $where = $this->getAdapter()->quoteInto("$this->_primary_name IN (?)", $aListID);
            $result = $this->delete($where);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $result;
    }
	
	public function printRopGetAll($params){
		$pagenum    = $params['pagenum'];
		$pagesize   = $params['pagesize'];
		$start      = $pagenum * $pagesize;
		$where = "1=1";
		$fromDate   = $params['fromDate'];
		$toDate     = $params['toDate'];
		if ($fromDate !='') {
			$objDate = new Clound_Datetime($fromDate, 'dd/MM/yyyy');
			$fromDate = $objDate->ToYYyymmdd();
			$where .= " And CreateDate >= '$fromDate'";
		}
		if ($toDate !='') {
			$objDate = new Clound_Datetime($toDate, 'dd/MM/yyyy');
			$objDate->addDay('1');
			$toDate = $objDate->ToYYyymmdd();
			$where .= " And CreateDate < '$toDate'";
		}
		// Filter
		if (isset($params['filterscount']))
		{
			$filterscount = $params['filterscount'];
			
			if ($filterscount > 0)
			{
				$where .= " And (";
				$tmpdatafield = "";
				$tmpfilteroperator = "";
				for ($i=0; $i < $filterscount; $i++)
				{
					// get the filter's value.
					$filtervalue = $params["filtervalue" . $i];
					// get the filter's condition.
					$filtercondition = $params["filtercondition" . $i];
					// get the filter's column.
					$filterdatafield = $params["filterdatafield" . $i];
					// get the filter's operator.
					$filteroperator = $params["filteroperator" . $i];
					
					if ($tmpdatafield == "")
					{
						$tmpdatafield = $filterdatafield;           
					}
					else if ($tmpdatafield <> $filterdatafield)
					{
						$where .= ")AND(";
					}
					else if ($tmpdatafield == $filterdatafield)
					{
						if ($tmpfilteroperator == 0)
						{
							$where .= " AND ";
						}
						else $where .= " OR ";  
					}
					
					// build the "WHERE" clause depending on the filter's condition, value and datafield.
					$where .= Clound_Public::getInstance()->getClauseByFilter($filtercondition, $filterdatafield, $filtervalue);
									
					if ($i == $filterscount - 1)
					{
						$where .= ")";
					}
					
					$tmpfilteroperator = $filteroperator;
					$tmpdatafield = $filterdatafield;      
				}
			}
		}
		// Sort
		$orders =' CreateDate DESC';
		if (isset($params['sortdatafield'])) {
			$sortfield = $params['sortdatafield'];
			$sortorder = $params['sortorder'];
			if ($sortorder != '') {
				if ($sortorder == "desc") {
					$orders = " " . $sortfield . " DESC";
				} else if ($sortorder == "asc") {
					$orders = " " . $sortfield . " ASC";
				}
			}
		}
		try {
			// Lay tong
			$getTotal = $this->select()->from($this, array())->columns(array('total' => new Zend_Db_Expr('COUNT(*)')))->where($where);
			$rows = $this->getAdapter()->fetchRow($getTotal);
			$total_rows = $rows['total'];
			// Lay du lieu
			$query = $this->select()->from($this)->where($where)->order($orders)->limit($pagesize, $start);
			$rows = $this->getAdapter()->fetchAll($query);
		} catch (Exception $e) {
			echo $e->getMessage();
		}
		if($total_rows>0){
			$oCacheFile = new Clound_CacheFile();
			$oCloundPublic = new Clound_Public();
			$arrUnit = $oCacheFile->getList('PHUONG_XA');
			$arrRecordType = $oCacheFile->getList('TTHC');
			$arrForm = $oCacheFile->getList('HINH_THUC');
			for($i=0;$i<$total_rows;$i++){
				//Don vi
				$rows[$i]['UnitName'] = $oCloundPublic->getNameByCode($arrUnit, $rows[$i]['Wards']);
				//Loai TTHC
				$rows[$i]['RecordType'] = $oCloundPublic->getNameByCode($arrRecordType, $rows[$i]['RecordType']);
				//Hinh thuc
				$rows[$i]['Form'] = $oCloundPublic->getNameByCode($arrForm, $rows[$i]['Form']);
				//Trang thai
				if($rows[$i]['Status'] == 'TIEP_NHAN'){
					$rows[$i]['Status'] = 'Chưa in';
				}else{
					$rows[$i]['Status'] = 'Đã in';
				}
			}
		}
		//var_dump($rows);exit;
		$data[] = array(
			'TotalRows' => $total_rows,
			'Rows' => $rows
		);
		return $data;	
	}
}
?>
